package cn.antscrpt.antLib.test 
{
	
/**
* Alternativa3D Maze Demo
* W,S,A,D+Spaceで移動
* ドラッグで方向回転
* 参考: http://clockmaker.jp/blog/2008/10/alternativa3d_walkcontroller/
*/


	import alternativa.engine3d.controllers.*;
	import alternativa.engine3d.core.*;
	import alternativa.engine3d.display.*;
	import alternativa.engine3d.materials.*;
	import alternativa.engine3d.primitives.*;
	import alternativa.types.*;
	import alternativa.utils.*;
	
	import flash.net.*;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;

	[SWF(frameRate="60")]
	
	public class EX12 extends Sprite
	{
		private const WIDTH_NUM:int = 10;
		private const HEIGHT_NUM:int = 10;
		private const LINE_SIZE:int = 500;
		
		private const MAZE_ARR:Array = 
		[
			[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
			[1, 0, 1, 0, 0, 1, 0, 0, 0, 1],
			[1, 0, 1, 0, 0, 1, 0, 0, 0, 1],
			[1, 0, 1, 1, 0, 1, 0, 1, 1, 1],
			[1, 0, 0, 0, 0, 0, 0, 1, 0, 1],
			[1, 0, 0, 0, 1, 0, 0, 1, 0, 1],
			[1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
			[1, 0, 1, 0, 1, 1, 1, 1, 0, 1],
			[1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
			[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
		]
		
		private var scene:Scene3D;
		private var view:View;
		private var camera:Camera3D;
		
		// The camera controller
		private var controller:WalkController;
		
		public function Main()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			// Creating scene
			scene = new Scene3D();
			scene.root = new Object3D();
			
			// create box
			for (var i:int = 0; i < MAZE_ARR.length; i++ )
			{
				for (var j:int = 0; j < MAZE_ARR[i].length; j++ )
				{
					if (MAZE_ARR[i][j] == 0) continue;
					
					var box:Box = Box(scene.root.addChild(new Box(LINE_SIZE, LINE_SIZE, LINE_SIZE)));
					box.cloneMaterialToAllSurfaces(new DevMaterial(0,0xFF4422));
					box.x = LINE_SIZE * (i - WIDTH_NUM / 2);
					box.y = LINE_SIZE * (j - HEIGHT_NUM / 2);
				}
			}
			var plane:Plane = Plane(scene.root.addChild(new Plane(LINE_SIZE * MAZE_ARR.length, LINE_SIZE * MAZE_ARR.length)));
			plane.cloneMaterialToAllSurfaces(new FillMaterial(0x221100));
			
			plane.z = -LINE_SIZE / 2;
			plane.rotationX = 0 * Math.PI / 180;
			
			// Adding camera and view
			camera = new Camera3D();
			camera.rotationX = MathUtils.toRadian(-90)
			camera.rotationZ = MathUtils.toRadian(120)
			scene.root.addChild(camera);

			view = new View();
			addChild(view);
			view.camera = camera;
			
			// 徒歩のコントローラーを作成
			controller = new WalkController(stage);
			
			// キーボード操作を可能にする機能
			controller.setDefaultBindings();
			// キーボードショートカットの整理
			controller.bindKey(KeyboardUtils.UP, ObjectController.ACTION_FORWARD);
			controller.bindKey(KeyboardUtils.DOWN, ObjectController.ACTION_BACK);
			controller.bindKey(KeyboardUtils.LEFT, ObjectController.ACTION_LEFT);
			controller.bindKey(KeyboardUtils.RIGHT, ObjectController.ACTION_RIGHT);
			
			// WalkControllerの対象を設定
			controller.object = camera;
			// 障害物の衝突設定
			controller.checkCollisions = true;
			// 徒歩の速度
			controller.speed = 500;
			// ジャンプ(スペースでジャンプできます)の速度
			controller.jumpSpeed = 750;
			// 重力の速度
			controller.gravity = 600;
			// 視点(制御点)の高さ
			controller.objectZPosition = 2;
			
			// FPS display launch
			FPS.init(stage);
			
			stage.addEventListener(Event.RESIZE, onResize);
			stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			onResize(null);
		}
		
		private function onEnterFrame(e:Event):void
		{
			// interface
			controller.processInput();
			
			// Scene calculating
			scene.calculate();
		}
		
		/**
		 * Resize Handler
		 */
		private function onResize(e:Event):void
		{
			view.width = stage.stageWidth;
			view.height = stage.stageHeight;
			
			// BackGround Color
			var bgMatrix:Matrix = new Matrix();
			bgMatrix.rotate(90 * Math.PI / 180);
			graphics.clear()
			graphics.beginGradientFill("linear", [0xFFFFFF, 0x001122], [100, 100], [0, 255], bgMatrix);
			graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
		}
		
	}
}